﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.Catering_Page.yl.Daily
{
    public partial class tionSupervision : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txt_stTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_endTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy/MM/dd'})");
                txt_stTime.Text = "全部";
                txt_endTime.Text = "全部";
                LoadDrop();
                LoadForm();
            }
        }
        protected void LoadDrop()
        {
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_group, isAdmin, groupid, companyid, true, false);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_group.SelectedItem, ddl_Class, isAdmin, classid, true);
            }
        }
        protected void ddl_group_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_group.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_group.SelectedItem, ddl_Class);
            }
        }

        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            //this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");            
        }
        //公用的DropDownList绑定方法
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }
        public DataSet SelTJRByDateAndClassID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 RID");
            strSql.Append(" FROM DailyManagement ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet SelPNameByRID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 Name");
            strSql.Append(" FROM Personnel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id = " + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public void LoadForm()
        {
            string strTmp = "";
            if(txt_SName.Text =="")
            {
                if( txt_stTime.Text == "全部" && txt_endTime.Text == "全部" )
                {   
                    if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text == "全部")
                        strTmp = " GroupID = '" + ddl_group.SelectedValue + "'";
                    if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text != "全部")
                        strTmp = " GroupID = '" + ddl_group.SelectedValue + "' and ClassID = '" + ddl_Class.SelectedValue +"'";
                }
                else if (txt_stTime.Text != "全部" && txt_endTime.Text != "全部")
                {
                    if (ddl_group.SelectedItem.Text == "全部" && ddl_Class.SelectedItem.Text == "全部")
                        strTmp = "AddDate between '" + txt_stTime.Text + "' and '" + txt_endTime.Text + "'";                   
                    if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text == "全部")
                        strTmp = " GroupID = '" + ddl_group.SelectedValue + "' and AddDate between '" + txt_stTime.Text + "' and '" + txt_endTime.Text + "'";
                    if (ddl_group.SelectedItem.Text != "全部" && ddl_Class.SelectedItem.Text != "全部")
                        strTmp = " GroupID = '" + ddl_group.SelectedValue + "' and ClassID = '" + ddl_Class.SelectedValue+ "' and AddDate between '" + txt_stTime.Text + "' and '" + txt_endTime.Text + "'";                    
                }
                else
                {
                    string strMsg = "alert('时间选择请一致，都为时间或都为全部！')";
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strMsg, true);
                }
            }
            else                         
                    strTmp = " ProName like '%" + txt_SName.Text + "%'";
            try
            {
                DataSet ds = new Catering_Rules.ROCProject().GetList(strTmp);
                DataTable dt_Plan = ds.Tables[0];
                dt_Plan.Columns.Add("tempId");
                dt_Plan.Columns.Add("ClassName");
                dt_Plan.Columns.Add("ItemsDate");
                dt_Plan.Columns.Add("PersonName");
                for (int i = 0; i < dt_Plan.Rows.Count; i++)
                {                    
                    dt_Plan.Rows[i]["tempId"] = i + 1;
                    dt_Plan.Rows[i]["ClassName"] = SelCNameByCID(dt_Plan.Rows[i]["ClassID"].ToString()).Tables[0].Rows[0][0];
                    dt_Plan.Rows[i]["ItemsDate"] = Convert.ToDateTime(dt_Plan.Rows[i]["AddDate"].ToString()).ToString("yyyy/MM/dd");

                    int strCount = 0;
                    for (int iCount = 0; iCount < dt_Plan.Rows[i]["PName"].ToString().Length; iCount++)
                    {
                        if (dt_Plan.Rows[i]["PName"].ToString()[iCount] == ';')
                            strCount++;
                    }
                    string strNameTmp = "";
                    for (int selCount = 0; selCount < strCount; selCount++)
                    { 
                        strNameTmp += SingleMembersByID(dt_Plan.Rows[i]["PName"].ToString().Split(';')[selCount].ToString()).Tables[0].Rows[0][0].ToString()+";";
                    }
                    dt_Plan.Rows[i]["PersonName"] = strNameTmp;
                }
                this.Rep_Plan.DataSource = dt_Plan;
                this.Rep_Plan.DataBind();
            }
            catch
            {

            }
        }
        public DataSet SingleMembersByID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id =" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet SelCNameByCID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName");
            strSql.Append(" FROM ClassInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id = " + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        protected void Search_Click(object sender, EventArgs e)
        {
            LoadForm();
        }

        protected void Rep_Plan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.ROCProject().Delete(delID))
                {
                    this.LoadForm();
                }
            }
        }
    }
}